<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Join_us extends MY_Controller {
	function __construct() {
		parent::__construct();

		$this->load->model('type_model', '', TRUE);
		$this->load->model('con_common_model', '', TRUE);
		$this->load->model('con_photo_model', '', TRUE);
		$this->load->model('con_team_model', '', TRUE);
		$this->load->model('position_model', '', TRUE);
		$this->load->model('region_model', '', TRUE);
	}
	
	public function common() {
		if ($this->input->is_ajax_request()) {
			$type = $this->input->post("type");
			$selector = $this->input->post("selector");
			$operate = $this->input->post("operate");
			$is_submit = ($this->input->post("is_submit") == 'false') ? FALSE : TRUE;
			$form_data['textarea'] = $this->input->post("form_textarea_data");
			
			if (!$is_submit) {
				eval("\$data = \$this->" . $type . "(\$type, \$selector, \$operate);");
				
				$data['selector'] = $selector;
				$data['operate'] = $operate;
				
				$data['selector_data'] = $this->_get_current_editable_obj($type, $selector);
				
				$this->load->view('backend/' . $this->parent_type . '/' . $type, $data);
			} else {
				eval("\$data = \$this->" . $type . "(\$type, \$selector, \$operate, true, \$form_data);");
				
				if (!empty($data['res'])) {
					$this->output->set_output(json_encode($data['res']));
				} else {
					$this->output->set_output(json_encode('success'));
				}
			}
		}
	}
	
	public function corporate_culture($type, $selector, $operate, $is_submit = FALSE, $form_data = NULL) {
		switch ($selector) {
			case selector_replace('.inner-content img'):
				if (!$is_submit) {
					$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
					
					$data['pic'] = $this->con_photo_model->search_by_condition($condition, TRUE);
				} else {
					$upload_data = $this->_upload_pic();
					
					/*echo "<pre>";
					print_r($upload_data);*/
					
					$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
					$pic = $this->con_photo_model->search_by_condition($condition, TRUE);
					
					$update_data['id'] = $pic['id'];
					$update_data['photo_path'] = $upload_data['file_db_path'];
					$update_data['upload_time'] = time();
					$this->con_photo_model->update($update_data);
				}
				break;
		}
		
		if (empty($data)) { $data = NULL; }
		
		return $data;
	}
	
	public function staff_activities($type, $selector, $operate, $is_submit = FALSE, $form_data = NULL) {
		switch ($selector) {
			case selector_replace('.staff_activities .center-content'):
				if ($operate == 'create') {
					if (!$is_submit) {
						
					} else {
						$upload_data = $this->_upload_pic();
						
						// save activity info
						$temp = $this->_get_info_by_filename($upload_data['orig_name'], array('name', 'sub_title'));
						
						$save_data = $temp;
						$save_data['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						
						$condition['type'] = $save_data['type'];
						$max_weight = $this->_get_max_weight($this->con_common_model->search_by_condition($condition));
						$save_data['weight'] = $max_weight + 1;
						
						$save_data['is_visible'] = TRUE;
						
						$activity_id = $this->con_common_model->save($save_data);
						
						// save activity photo
						$save_data2['con_id'] = $activity_id;
						$save_data2['type'] = $save_data['type'];
						$save_data2['thumb_path'] = $upload_data['file_db_path'];
						$save_data2['upload_time'] = time();
						$save_data2['weight'] = 1;
						$save_data2['is_visible'] = TRUE;
						$this->con_photo_model->save($save_data2);
					}
				} elseif ($operate == 'edit') {
					if (!$is_submit) {
						$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						$data['activities'] = $this->con_common_model->search_by_condition($condition);
					} else {
						$this->_update_weight($this->input->post("activities_weight"), 'common');
					}
				} elseif ($operate == 'delete') {
					$this->_delete_item('common');	
				}
				break;
			case selector_replace('.inner-content .activities'):
				if ($operate == 'create') {
					if (!$is_submit) {
						$activity_id = $this->input->post("activity_id");
						$_SESSION['current_edit']['activity_id'] = $activity_id;
					} else {
						$activity_id = $_SESSION['current_edit']['activity_id'];
						
						$upload_data = $this->_upload_pic();
						
						// save activity photo
						$save_data['con_id'] = $activity_id;
						$save_data['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						$save_data['photo_path'] = $upload_data['file_db_path'];
						$save_data['upload_time'] = time();
						
						$condition['con_id'] = $activity_id;
						$condition['type'] = $save_data['type'];
						$max_weight = $this->_get_max_weight($this->con_photo_model->search_by_condition($condition));
						$save_data['weight'] = $max_weight + 1;
						
						$save_data['is_visible'] = TRUE;
						
						$this->con_photo_model->save($save_data);
					}
				} elseif ($operate == 'edit') {
					if (!$is_submit) {
						$activity_id = $this->input->post("activity_id");
						
						$condition['con_id'] = $activity_id;
						$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						$data['pic'] = $this->con_photo_model->search_by_condition($condition);
					} else {
						$this->_update_weight($this->input->post("activity_pic_weight"), 'photo');
					}
				} elseif ($operate == 'delete') {
					$this->_delete_item('photo');
				}
				break;
			case selector_replace('#course_modal #gallery'):
				if ($operate == 'create') {
					if (!$is_submit) {
						$course_id = $this->input->post("course_id");
						$_SESSION['current_edit']['course_id'] = $course_id;
					} else {
						$course_id = $_SESSION['current_edit']['course_id'];
						
						$upload_data = $this->_upload_pic();
						
						// save course photo
						$save_data['con_id'] = $course_id;
						$save_data['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						$save_data['photo_path'] = $upload_data['file_db_path'];
						$save_data['upload_time'] = time();
						
						$condition['con_id'] = $course_id;
						$condition['type'] = $save_data['type'];
						$max_weight = $this->_get_max_weight($this->con_photo_model->search_by_condition($condition));
						$save_data['weight'] = $max_weight + 1;
						
						$save_data['is_visible'] = TRUE;
						
						$this->con_photo_model->save($save_data);
					}
				} elseif ($operate == 'edit') {
					if (!$is_submit) {
						$course_id = $this->input->post("course_id");
						
						$condition['con_id'] = $course_id;
						$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						$data['pic'] = $this->con_photo_model->search_by_condition($condition, TRUE);
					} else {
						$this->_update_weight($this->input->post("course_pic_weight"), 'photo');
					}
				} elseif ($operate == 'delete') {
					$this->_delete_item('photo');
				}
				break;
		}
		
		if (empty($data)) { $data = NULL; }
		
		return $data;
	}
	
	public function contact($type, $selector, $operate, $is_submit = FALSE, $form_data = NULL) {
		switch ($selector) {
			case selector_replace('.inner-content img'):
				if (!$is_submit) {
					$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
					
					$data['pic'] = $this->con_photo_model->search_by_condition($condition, TRUE);
				} else {
					$upload_data = $this->_upload_pic();
					
					/*echo "<pre>";
					print_r($upload_data);*/
					
					$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
					
					$pic = $this->con_photo_model->search_by_condition($condition, TRUE);
					
					$update_data['id'] = $pic['id'];
					$update_data['photo_path'] = $upload_data['file_db_path'];
					$update_data['upload_time'] = time();
					$this->con_photo_model->update($update_data);
				}
				break;
			case selector_replace(''):
				break;
		}
		
		if (empty($data)) { $data = NULL; }
		
		return $data;
	}
	
	public function company_location($type, $selector, $operate, $is_submit = FALSE, $form_data = NULL) {
		switch ($selector) {
			case selector_replace('.inner-content img'):
				if (!$is_submit) {
					$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
					
					$data['pic'] = $this->con_photo_model->search_by_condition($condition, TRUE);
				} else {
					$upload_data = $this->_upload_pic();
					
					/*echo "<pre>";
					print_r($upload_data);*/
					
					$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
					
					$pic = $this->con_photo_model->search_by_condition($condition, TRUE);
					
					$update_data['id'] = $pic['id'];
					$update_data['photo_path'] = $upload_data['file_db_path'];
					$update_data['upload_time'] = time();
					$this->con_photo_model->update($update_data);
				}
				break;
		}
		
		if (empty($data)) { $data = NULL; }
		
		return $data;
	}
	
	public function careers($type, $selector, $operate, $is_submit = FALSE, $form_data = NULL) {
		switch ($selector) {
			case selector_replace('.inner-content img'):
				if (!$is_submit) {
					$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
					
					$data['pic'] = $this->con_photo_model->search_by_condition($condition, TRUE);
				} else {
					$upload_data = $this->_upload_pic();
					
					/*echo "<pre>";
					print_r($upload_data);*/
					
					$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
					
					$pic = $this->con_photo_model->search_by_condition($condition, TRUE);
					
					$update_data['id'] = $pic['id'];
					$update_data['photo_path'] = $upload_data['file_db_path'];
					$update_data['upload_time'] = time();
					$this->con_photo_model->update($update_data);
				}
				break;
		}
		
		if (empty($data)) { $data = NULL; }
		
		return $data;
	}
}